// subpkg_worker/prescription/prescription.js
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    medicationId:'',
    elderlyId:'',
    bg1:'#c00000',
    activeName: '1',
    term:'长期',
    bg2:'white',
    elderlyId:'20',
    display1:'block',
    display2:'none',
    medicationList:[],
    prescriptionList_short:
    [
    ],
    prescriptionList_long:
    [
    ]
  },
  change_short(){
    this.setData({
      term:'长期',
      bg1:'#c00000',
      bg2:'white',
      display1:'block',
      display2:'none'
    })
  },
  change_long(){
    this.setData({
      term:'短期',
      bg1:'white',
      bg2:'#c00000',
      display1:'none',
      display2:'block'
    })
  },
  onChange(event) {
    this.setData({
      activeName: event.detail,
    });
  },
 
  addPrescription(){
    // var medicineList = this.data.medicineList
    // var medicineList_json=JSON.stringify(medicineList)
    wx.navigateTo({
      url: `/subpkg_worker/prescriptionAdd/prescriptionAdd?elderId=${this.data.elderlyId}`,
    })
  },
  deletePrescription(){
    wx.navigateTo({
      url: `/subpkg_worker/prescriptionDel/prescriptionDel?elderId=${this.data.elderlyId}`,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  
  onLoad(options) {
    if(options.medicationId!=undefined&&options.time!=undefined&&options.elderId!=undefined){
      this.setData({
        medicationId:options.medicationId,
        time:options.time,
        elderlyId:options.elderId
      })
    }
   

    
    this.setData({
      elderlyId:options.elderId
    })
    //生成用药记录
    // wx.request({
    //   url: 'https://zhixiao.site:8080/medication-record/generate',
    //   success:(res)=>{
    //     console.log("success")
    //   },
    //   complete:(res)=>{
    //     //获取老人正在服用的药单项
    //     this.query1()
    //   }
    // })
    //获取老人正在服用的药单项
    this.query1()
  },
  
  query1(){
    wx.request({
      url: 'https://zhixiao.site:8080/client/medication_record/selectToday',
      data:{
        elderlyId:this.data.elderlyId
      },
      success:(res)=>{
        var medicineList = []
        var medicineList1 = res.data.data
        console.log(medicineList)
        for (var i in medicineList1){
          console.log(medicineList1[i])
          var id = medicineList1[i].id
          var medicationId = medicineList1[i].medicationList.medication.id
          var term = medicineList1[i].medicationList.term
          var name = medicineList1[i].medicationList.medication.name
          var type = medicineList1[i].medicationList.medication.type
          var dosage = medicineList1[i].medicationList.medication.dosage
          var frequency = medicineList1[i].medicationList.medication.frequency.frequency
          var mode = medicineList1[i].medicationList.medication.mode
          var unit = medicineList1[i].medicationList.medication.unit
          var time = medicineList1[i].time.slice(0,5)
          var status = medicineList1[i].status
          medicineList.push({id:id,name:name,time:time,status:status,type:type,dosage:dosage,frequency:frequency,mode:mode,unit:unit,term:term,medicationId:medicationId})
        }
        this.setData({
          medicineList:medicineList
        })
      },
      complete:(res)=>{
        var medicineList =  this.data.medicineList
        for(var i in medicineList){
          if(medicineList[i].medicationId===this.data.medicationId&&medicineList[i].time===this.data.time.slice(0,5)){
            this.setData({
              activeName:parseInt(i)+1
            })
            if(medicineList[i].term!='长期'){
              this.change_long()
            }
            break
          }
        }
      }
    })
  },
  //更新用药
  record(e){
    var index = e.currentTarget.dataset.index
    console.log(index)
    var medication = this.data.medicineList[index]
    var id = this.data.medicineList[index].id
    wx.request({
      url: `https://zhixiao.site:8080/client/medication_record/update?id=${id}&status=1`,
      method:'PUT',
      // header:{
      //   'Content-Type':'application/json'
      // },
      // data:
      // {
      //   id:this.data.listId,
      //   status:0
      // },
      success:(res)=>{
        Toast(`${medication.name}  ${medication.time}  服用成功!`);
        console.log(res)
      },
      complete:(res)=>{
        this.query1()
      }
    })
  
 
},
  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow(){
     //生成用药记录
     wx.request({
      url: 'https://zhixiao.site:8080/medication-record/generate',
      success:(res)=>{
        console.log("success")
      },
      complete:(res)=>{
        //获取老人正在服用的药单项
        this.query1()
      }
    })

     


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})